﻿import {ActionContext} from 'vuex';
import ListModule from '@/store/modules/list-module';
import ListState from '@/store/modules/list-state';
import PageResult from '@/store/entities/page-result';
import Ajax from '@/lib/ajax';
import Dictionary from '@/store/entities/administration/dictionary/dictionary';

interface DictionaryState extends ListState<Dictionary> {
    dictionaryForEdit: any,
    excelFile: any,
    dictionary: any,
    dictionariesByCategoryCode: any,
}

class DictionaryModule extends ListModule<DictionaryState, any, Dictionary> {
    state = {
        totalCount: 0,
        currentPage: 1,
        pageSize: 10,
        list: new Array<Dictionary>(),
        loading: false,
        dictionaryForEdit: {},
        excelFile: {},
        dictionary: {},
        dictionariesByCategoryCode: new Array<Dictionary>(),
    };
    actions = {
        async obtainDictionaries(context: ActionContext<DictionaryState, any>, payload: any) {
            context.state.loading = true;
            const response = await Ajax.post('/api/services/app/Dictionary/ObtainDictionaries', payload.data);
            context.state.loading = false;
            const result = response.data.result as PageResult<Dictionary>;
            context.state.totalCount = result.totalCount;
            context.state.list = result.items;
        },
        async createOrUpdateDictionary(context: ActionContext<DictionaryState, any>, payload: any) {
            await Ajax.post('/api/services/app/Dictionary/CreateOrUpdateDictionary', payload.data);
        },
        async deleteDictionary(context: ActionContext<DictionaryState, any>, payload: any) {
            await Ajax.delete('/api/services/app/Dictionary/DeleteDictionary?Id=' + payload.data.id);
        },
        async getDictionaryForEdit(context: ActionContext<DictionaryState, any>, payload: any) {
            const response = await Ajax.get('/api/services/app/Dictionary/GetDictionaryForEdit?Id=' + payload.data.id);
            context.state.dictionaryForEdit = response.data.result;
        },
        async getDictionary(context: ActionContext<DictionaryState, any>, payload: any) {
            const response = await Ajax.get('/api/services/app/Dictionary/GetDictionary?Id=' + payload.data.id);
            context.state.dictionary = response.data.result;
        },
        async obtainDictionariesToExcel(context: ActionContext<DictionaryState, any>, payload: any) {
            const response = await Ajax.post('/api/services/app/Dictionary/ObtainDictionariesToExcel', payload.data);
            context.state.excelFile = response.data.result;
        },
        async getDictionariesByCategoryCode(context: ActionContext<DictionaryState, any>, payload: any) {
            const response = await Ajax.get('/api/services/app/Dictionary/GetDictionariesByCategoryCode', {params: payload.data});
            context.state.dictionariesByCategoryCode = response.data.result.items;
        },
    };
    mutations = {
        setCurrentPage(state: DictionaryState, page: number) {
            state.currentPage = page;
        },
        setPageSize(state: DictionaryState, pageSize: number) {
            state.pageSize = pageSize;
        },
    };
}

const dictionaryModule = new DictionaryModule();
export default dictionaryModule;
